package day07.object;

/**
 * 学生类
 *
 * @since 09:36
 * @author wuguidong@cskaoyan.onaliyun.com
 */
public class Student {
    private String name;
    private int id;
    private int score;
    private String grade;

    // 计数器,统计外部创建对象的次数
    private static int count = 0;

    // 私有化构造器,避免外界无限制的创建对象
    private Student(String name, int id, int score, String grade) {
        this.name = name;
        this.id = id;
        this.score = score;
        this.grade = grade;
    }

    // 在类的外部已经不能new对象了,所以考虑在类的内部提供方法来对外创建Student对象,并且控制创建对象的次数
    /*
        这个方法的修饰符应该是 public static
        返回值类型必须是Student类型,方法返回一个Student对象
     */
    public static Student getInstance(String name, int id, int score, String grade) {
        // 开发中,边界值的测试是最关键的
        if (count < 10) {
            // 每对外提供一次对象,计数器 + 1
            count++;
            return new Student(name, id, score, grade);
        }
        return null;
    }

    // 打印对象的属性
    public void printStu() {
        System.out.println(name + ", " + id + ", " + score + ", " + grade);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public String getGrade() {
        return grade;
    }

    public void setGrade(String grade) {
        this.grade = grade;
    }
}
